package com.gin.no_gay_alliance.adpter.pm;

import android.content.Context;
import android.content.DialogInterface;
import android.view.View;

import androidx.annotation.NonNull;

import com.gin.jackson.utils.ObjectUtils;
import com.gin.nga.method.NukeApi;
import com.gin.nga.response.field.SimpleUserInfo;
import com.gin.no_gay_alliance.R;
import com.gin.no_gay_alliance.adpter.base.BaseBindingRecyclerViewAdapter;
import com.gin.no_gay_alliance.app.MyApplication;
import com.gin.no_gay_alliance.callback.RequestCallback;
import com.gin.no_gay_alliance.databinding.ItemPriMsgBlockBinding;
import com.gin.no_gay_alliance.utils.AlertDialogUtils;

import java.util.List;

/**
 * 私信黑名单适配器
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/15 10:57
 **/
public class PriMsgBlockAdapter extends BaseBindingRecyclerViewAdapter<ItemPriMsgBlockBinding, SimpleUserInfo> implements View.OnClickListener {
    public PriMsgBlockAdapter(Context context) {
        super(context);
    }

    public PriMsgBlockAdapter(@NonNull Context context, @NonNull List<SimpleUserInfo> data) {
        super(context, data);
    }

    /**
     * 绑定数据
     *
     * @param holder
     * @param b        绑定对象
     * @param position 位置
     * @param item
     */
    @Override
    protected void onBindViewHolder(ViewHolder<ItemPriMsgBlockBinding> holder, @NonNull ItemPriMsgBlockBinding b, int position, SimpleUserInfo item) {
        String avatar = !ObjectUtils.isEmpty(item.getAvatars()) ? item.getAvatars().get(0) : "";
        b.tvUser.init(item.getUserId(), item.getUsername(), avatar, 36, 0);

        b.ivDelete.setTag(item);
        b.ivDelete.setOnClickListener(this);
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        // 删除黑名单
        if (v.getId() == R.id.iv_delete) {
            SimpleUserInfo user = (SimpleUserInfo) v.getTag();
            int index = data.indexOf(user);
            AlertDialogUtils.showConfirm(context, "移除黑名单", String.format("确定将 %s 移出黑名单？", user.getUsername()), (dialog, which) -> {
                if (which == DialogInterface.BUTTON_POSITIVE) {
                    NukeApi.pmBlockDel(MyApplication.getClient(), user.getUserId()).async(new RequestCallback<>(d -> {
                        handler.toast(d.getMessage());
                        data.remove(index);
                        handler.post(() -> notifyItemRemoved(index));
                    }));
                }
            });
        }
    }

}
